package com.example.core.utils

import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import androidx.annotation.ColorInt
import java.util.regex.Pattern

/**
 * Created by zhudongao
 * Desc
 * Date 2020/12/5
 */
object SpanUtils {

    fun matchSearchText(@ColorInt color: Int, text: String, key: String): SpannableString {
        val spannableString = SpannableString(text)
        val pattern = Pattern.compile(key)
        val matcher = pattern.matcher(spannableString)
        while (matcher.find()) {
            val start = matcher.start()
            val end = matcher.end()
            spannableString.setSpan(ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        }
        return spannableString
    }
}